package com.example.discover.navigation

import androidx.navigation.NavHostController
import androidx.navigation.NavOptionsBuilder
import com.example.featuresdk.navigation.INavigationController

/**
 * Utility controller to manage navigation in one central place
 */
class NavigationController: INavigationController {

    private constructor()
    var navController: NavHostController? = null
        internal set

    companion object{
        val instance = NavigationController()
    }

    override fun navigate(route: String, vararg params: String, builder: NavOptionsBuilder.() -> Unit) {
        Screen.values().lastOrNull { it.route == route }?.let {
            navigate(it, *params){ builder() }
        }
    }

    override fun back(){
        navController?.popBackStack()
    }

    override fun getNavigationRoutes(): List<String> {
        return Screen.values().map {
            it.route
        }
    }

    private fun navigate(screen: Screen, vararg params: String, builder: NavOptionsBuilder.() -> Unit = {launchSingleTop = true}) {
        when(screen){

            else ->
                navController?.navigate(screen.route){
                    launchSingleTop = true
                }
        }
    }
}